package scala.day03

import scala.io.Source

object Demo02StudentTest {
  def main(args: Array[String]): Unit = {
    /**
      * 统计学生的总分
      */
    //读取分数表
    val scores: List[String] = Source.fromFile("data/score.txt").getLines().toList
    //取出学号和分数
    val idAndScore: List[(String, Double)] =scores.map(score => {
      val split: Array[String] = score.split(",")
      (split(0),split(2).toDouble)
    })
    //按照学号分组
    val group: Map[String, List[(String, Double)]] = idAndScore.groupBy(i => i._1)
    //统计学生的总分
    val idAndSum: Map[String, Double] = group.map(kv => {
      val id: String = kv._1
      //取出一个人所有的分数
      val doubles: List[Double] = kv._2.map(kv1 => kv1._2)
      //计算总分
      val sum: Double = doubles.sum
      (id,sum)
    })
    idAndSum.toList.sortBy(f => f._1).foreach(println)
  }
}
